package forms;

import java.util.Calendar;

import baseclasses.Course;
import com.devcon5.StudentPlanner.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

public class CourseForm extends Activity {
	
	//Views, layouts, and String variables
	protected EditText courseNameEditText, courseCreditsEditText, courseLocationEditText;
	protected LinearLayout courseWeekDaysLayout, courseTimeLayout, courseEndTimeLayout;
	protected TextView courseDaysText, courseTimeText, courseEndTimeText;
	protected Button saveButton, cancelButton;
	protected String[] weekDayStrings = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
	protected Context context = this;
	
	//Variables used by Add and Edit functionality
	protected boolean[] weekDayBooleans;
	protected Calendar startTimeCalendar;
	protected Calendar endTimeCalendar;
	
	//Variables for persisting data on orientation change
	protected static final String COURSE_TIME_LONG = "COURSE_TIME_LONG";
	protected static final String COURSE_END_TIME_LONG = "COURSE END TIME LONG";
	protected static final String WEEK_DAY_BOOLEANS = "WEEK_DAY_BOOLEANS";
	protected long courseTimeLong;
	protected long endCourseTimeLong;
	
	protected boolean endTimeSelected = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcourse);
		
		//Initialize all variables
		courseNameEditText = (EditText)findViewById(R.id.addcourse_coursenameedittext);
		courseWeekDaysLayout = (LinearLayout)findViewById(R.id.addcourse_coursedayslayout);
		courseWeekDaysLayout.setBackgroundResource(android.R.drawable.list_selector_background);
		courseTimeLayout = (LinearLayout)findViewById(R.id.addcourse_timelayout);
		courseTimeLayout.setBackgroundResource(android.R.drawable.list_selector_background);
		saveButton = (Button)findViewById(R.id.addcourse_addbutton);
		cancelButton = (Button)findViewById(R.id.addcourse_cancelbutton);
		courseDaysText = (TextView)findViewById(R.id.addcourse_coursedays);
		courseTimeText = (TextView)findViewById(R.id.addcourse_time);
		courseCreditsEditText = (EditText)findViewById(R.id.addcourse_credits);
		courseLocationEditText = (EditText)findViewById(R.id.addcourse_location);
		courseEndTimeLayout = (LinearLayout)findViewById(R.id.addcourse_endtimelayout);
		courseEndTimeText = (TextView)findViewById(R.id.addcourse_endtime);
		
		
	
		if(savedInstanceState!=null)	//If orientation was changed, reset values that were in display
		{
			courseTimeLong = savedInstanceState.getLong(COURSE_TIME_LONG);
			startTimeCalendar = Calendar.getInstance();
			startTimeCalendar.setTimeInMillis(courseTimeLong);
			endCourseTimeLong = savedInstanceState.getLong(COURSE_END_TIME_LONG);
			endTimeCalendar = Calendar.getInstance();
			endTimeCalendar.setTimeInMillis(endCourseTimeLong);
			weekDayBooleans = savedInstanceState.getBooleanArray(WEEK_DAY_BOOLEANS);
			courseDaysText.setText(Course.getWeekDays(weekDayBooleans));
			
		}
		
		
		
		//Set onClickListeners for each Layout item
		courseWeekDaysLayout.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
				onCourseWeekDaysLayoutClicked();
			}
		});
		
		courseTimeLayout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				onCourseTimeLayoutClicked(v);
			}
		});
		
		courseEndTimeLayout.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
				onCourseEndTimeLayoutClicked(v);
			}
		});
		
		saveButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				onSaveButtonClicked();
			}
		});
		
		cancelButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				onCancelButtonClicked();
			}
		});
		
		
	}
	
	
	
	protected void onCourseWeekDaysLayoutClicked(){
		
		final boolean[] tempBooleans = weekDayBooleans.clone();	//need a new copy of weekDayBooleans to set choices
	
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Week Days");
		builder.setMultiChoiceItems(weekDayStrings, weekDayBooleans, new OnMultiChoiceClickListener() {
			
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				weekDayBooleans[which] = isChecked;
			}
		});
		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
			
			public void onClick(DialogInterface arg0, int arg1) {
				courseDaysText.setText(Course.getWeekDays(weekDayBooleans));	
			}
		});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				weekDayBooleans = tempBooleans;
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
		
	}
	
	//Methods to be overridden by child classes (Add/Edit Course)
	//-----------------------------------------------------------
	protected void onCourseTimeLayoutClicked(View v){	//If the course start time layout is clicked, change the calendar variables to the input values
		
		v.setBackgroundResource(android.R.drawable.list_selector_background);
		new TimePickerDialog(context, new OnTimeSetListener(){
		
			public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
				startTimeCalendar.set(Calendar.HOUR_OF_DAY, arg1);
				startTimeCalendar.set(Calendar.MINUTE, arg2);
				courseTimeText.setText(Course.getStartTimeString(startTimeCalendar, "hh:mm a"));
			}
		}, startTimeCalendar.get(Calendar.HOUR_OF_DAY), startTimeCalendar.get(Calendar.MINUTE), false).show();
		
	}
	
	protected void onCourseEndTimeLayoutClicked(View v){ //If the course end time layout is clicked, change the calendar variables to the input values
		
		v.setBackgroundResource(android.R.drawable.list_selector_background);
		new TimePickerDialog(context, new OnTimeSetListener(){
		
			public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
				endTimeCalendar.set(Calendar.HOUR_OF_DAY, arg1);
				endTimeCalendar.set(Calendar.MINUTE, arg2);
				courseEndTimeText.setText(Course.getEndTimeString(endTimeCalendar, "hh:mm a"));
			}
		}, endTimeCalendar.get(Calendar.HOUR_OF_DAY), endTimeCalendar.get(Calendar.MINUTE), false).show();
		
	}
	
	//Methods to be overridden by child classes (Add/Edit Course)
	//-----------------------------------------------------------
	protected void onSaveButtonClicked(){}
	//overridden by child class
	
	
	protected void onCancelButtonClicked(){
		onBackPressed();
		finish();
	}
	
	//check if the course name is valid (not blank)
	protected boolean courseNameValid(String name){
		if(name.length()>0){
			return true;
		}
		return false;
	}
	
	//check if at least 1 weekday was selected
	protected boolean weekDaysValid(boolean[] weekDayBooleans){
		for(int i=0; i < weekDayBooleans.length;i++){
			if(weekDayBooleans[i]==true){
				return true;
			}
		}
		return false;
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {	//for orientation changes
		
		outState.putLong(COURSE_TIME_LONG, startTimeCalendar.getTimeInMillis());
		outState.putLong(COURSE_END_TIME_LONG, endTimeCalendar.getTimeInMillis());
		outState.putBooleanArray(WEEK_DAY_BOOLEANS, weekDayBooleans);
		
		super.onSaveInstanceState(outState);
	}
}
